package com.itheima.plusstudentsystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class PlusStudentSystem {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作 1.登录 2.注册 3.忘记密码 4.退出");
            System.out.println("请输入要选择的操作");
            String choose = sc.next();
            switch (choose) {
                case "1" -> login(list);
                case "2" -> register(list);
                case "3" -> forgetPassword(list);
                case "4" -> {
                    System.out.println("退出");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    private static void forgetPassword(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        boolean flag = contains(list, username);
        if(!flag){
            System.out.println("当前用户"+username+"未注册");
            return;
        }
        System.out.println("请输入身份证号码");
        String personId = sc.next();
        System.out.println("请输入手机号码");
        String phoneNumber = sc.next();
        int index=findIndex(list,username);
        User user = list.get(index);
        //比较用户对象中的手机号码和身份证号码是否相同
        if(!(user.getPersonId().equalsIgnoreCase(personId) && user.getPhoneNumber().equals(phoneNumber))){
            System.out.println("身份证号码或手机号码输入有误，不能修改密码");
            return;
        }
        String password;
        while (true) {
            System.out.println("请输入新的密码");
            password = sc.next();
            System.out.println("请再次输入新的密码");
            String againPassword = sc.next();
            if(password.equals(againPassword)){
                System.out.println("两次密码输入一致");
                break;
            }else{
                System.out.println("两次密码输入不一致，请重新输入");
            }
        }

        //直接修改即可
        user.setPassword(password);
        System.out.println("密码修改成功");
    }

    private static int findIndex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUsername().equals(username)){
                return i;
            }
        }
        return -1;
    }

    private static void login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名");
            String username = sc.next();
            boolean flag = contains(list, username);
            if (!flag) {
                System.out.println("用户名未注册，请先注册");
                return;
            }
            System.out.println("请输入密码");
            String password = sc.next();
            while (true) {
                String rightCode = getCode();
                System.out.println(rightCode);
                System.out.println("请输入验证码");
                String code = sc.next();
                //验证码判断时忽略大小写
                if (!rightCode.equalsIgnoreCase(code)) {
                    System.out.println("验证码不正确，请重新输入验证码");
                } else {
                    break;
                }
            }
            //定义一个方法验证用户名和密码是否正确
            //封装思想的应用：
            //我们可以把一些零散的数据，封装成一个对象
            //以后传递参数的时候，只要传递一个整体就可以了，不需要管这些零散的数据。
            User user = new User(username, password, null, null);
            boolean flag1 = checkUserInfo(list, user);
            if (flag1) {
                System.out.println("登录成功");
                StudentSystem ss = new StudentSystem();
                ss.startStudentSystem();
            } else {
                System.out.println("登录失败，用户名或密码错误");
                if(i==2){
                    System.out.println("账号已被锁定，请联系客服小姐姐");
                }else {
                    System.out.println("你还剩"+(2-i)+"次机会");
                }

            }
        }
    }

    private static boolean checkUserInfo(ArrayList<User> list, User user) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (user.getPassword().equals(u.getPassword()) && user.getUsername().equals(u.getUsername())) {
                return true;
            }
        }
        return false;
    }

    private static String getCode() {
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            Character c = list.get(index);
            sb.append(c);
        }
        int i = r.nextInt(10);
        sb.append(i);
        char[] chars = sb.toString().toCharArray();
        int index = r.nextInt(chars.length);
        char temp = chars[index];
        chars[index] = chars[chars.length - 1];
        chars[chars.length - 1] = temp;
        return new String(chars);
    }


    private static void register(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        String username;
        //判断用户名是否符合要求
        while (true) {
            System.out.println("请输入用户名");
            username = sc.next();
            //开发细节：先验证格式是否正确，再验证是否唯一
            //因为在以后所有的数据，都是存在数据库中，如果我们要校验，需要使用到网络资源。
            boolean flag = checkUsername(username);
            if (!flag) {
                System.out.println("用户格式不满足条件，需要重新输入");
                continue;
            }
            //代码执行到这里表示用户名格式是正确的，接下来判断用户名是否唯一
            boolean flag1 = contains(list, username);
            if (flag1) {
                System.out.println("用户名" + username + "已存在，请重新输入");
            } else {
                System.out.println("用户名" + username + "可用");
                break;
            }
        }
        //密码键盘输入两次，两次一致才可以进行注册。
        String password;
        while (true) {
            System.out.println("请输入密码");
            password = sc.next();
            System.out.println("请再次输入密码");
            String againPassword = sc.next();
            if (!password.equals(againPassword)) {
                System.out.println("两次输入的密码不一致，请重新输入");
                continue;
            }
            System.out.println("两次密码一致，继续录入其他数据");
            break;
        }
        /*身份证号码需要验证。
        验证要求：
        长度为18位
        不能以0为开头
        前17位，必须都是数字
        最为一位可以是数字，也可以是大写X或小写x*/
        String personId;
        while (true) {
            System.out.println("请输入身份证号");
            personId = sc.next();
            boolean flag = checkPersonId(personId);
            if (flag) {
                System.out.println("身份证号满足要求，请继续录入下一个数据");
                break;
            } else {
                System.out.println("身份证号有误，请重新输入");
            }
        }
        /*手机号验证
        验证要求：
        长度为11位
        不能以0为开头
        必须都是数字*/
        String phoneNumber;
        while (true) {
            System.out.println("请输入手机号码");
            phoneNumber = sc.next();
            boolean flag = checkPhoneNumber(phoneNumber);
            if (!flag) {
                System.out.println("该手机号码不满足要求，请重新输入");
                continue;
            }
            System.out.println("手机号码格式正确");
            break;

        }
        User user = new User(username, password, personId, phoneNumber);
        list.add(user);
        System.out.println("注册成功");
        printList(list);
    }

    private static void printList(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            System.out.println(user.getUsername() + ", " + user.getPassword() + ", "
                    + user.getPersonId() + ", " + user.getPhoneNumber());
        }
    }

    private static boolean checkPhoneNumber(String phoneNumber) {
        if (phoneNumber.length() != 11) {
            return false;
        }
        if (phoneNumber.startsWith("0")) {
            return false;
        }
        for (int i = 0; i < phoneNumber.length(); i++) {
            char c = phoneNumber.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;
    }

    private static boolean checkPersonId(String personId) {
        int len = personId.length();
        if (len != 18) {
            return false;
        }
        if (personId.startsWith("0")) {
            return false;
        }
        for (int i = 0; i < personId.length() - 1; i++) {
            char c = personId.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        char c = personId.charAt(personId.length() - 1);
        return (c >= '0' && c <= '9') || c == 'x' || c == 'X';
    }

    private static boolean checkUsername(String username) {
        int len = username.length();
        if (len < 3 || len > 15) {
            return false;
        }
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                return false;
            }
        }
        int count = 0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                count++;
                break;
            }
        }
        return count != 0;
    }

    private static boolean contains(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User rightUser = list.get(i);
            if (rightUser.getUsername().equals(username)) {
                return true;
            }
        }
        return false;
    }
}
